package com.srlg.common.util;

import com.j256.simplemagic.ContentInfo;
import com.j256.simplemagic.ContentInfoUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.http.MediaType;

import java.io.File;
import java.io.FileInputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MinioUtils {
  // 根据扩展名取出mimeType
  public static String getMimeType(String extension) {
    if (extension == null) {
      extension = "";
    }
    ContentInfo extensionMatch = ContentInfoUtil.findExtensionMatch(extension);
    String mimeType = MediaType.APPLICATION_OCTET_STREAM_VALUE;//通用mimeType，字节流
    if (extensionMatch != null) {
      mimeType = extensionMatch.getMimeType();
    }
    return mimeType;
  }

  //格式化年/月/日 用来作为文件存储目录 分片不用这个 分片用md5
  public static String getFormatTime() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    String folder = sdf.format(new Date()).replace("-", "/") + "/";
    return folder;
  }

  // md5加密文件
  public static String getFileMd5(File file) {
    try (FileInputStream fileInputStream = new FileInputStream(file)) {
      String fileMd5 = DigestUtils.md5Hex(fileInputStream);
      return fileMd5;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    }
  }
}
